//
//  MyVC.swift
//  SouWeiLaiSwift
//
//  Created by yxl on 2022/4/12.
//

import UIKit
import Foundation
import QMUIKit
import RxSwift
import RxCocoa

class MyVC: BaseViewController {
    let diposebag = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initAction()
        initLayout()
    }
    
    //MARK: - UI
    func initUI(){
        self.title = "我的"
        self.view.addSubview(loginOutButton)
        self.view.addSubview(queryButton)
    }
    
    //MARK: - 交互
    func initAction(){
        // 退出登录
        loginOutButton.rx.tap.subscribe(onNext: {
            XLAppDelegate.switchRootToLoginVC()
            UserManager.sharedManager.clearUserModel()
        }).disposed(by: diposebag)
        
        // 查询
        queryButton.rx.tap.subscribe(onNext: {
            let loginModel = RealmTools.querWithPrimaryKey(ofType:LoginModel.self, primaryKey: "6102574") as? LoginModel
            Log(loginModel)
        }).disposed(by: diposebag)
    }
    
    //MARK: - Layout
    func initLayout(){
        // 退出登录
        loginOutButton.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(20);
            make.right.equalTo(self.view).offset(-20);
            make.bottom.equalTo(self.view).offset(-50 - kTabBarHeight)
            make.height.equalTo(40)
        }
        // 查询
        queryButton.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(20);
            make.right.equalTo(self.view).offset(-20);
            make.bottom.equalTo(self.loginOutButton).offset(-50)
            make.height.equalTo(40)
        }
    }
    
    
    //MARK: - 懒加载
    //退出登录
    lazy var loginOutButton : QMUIButton = {
        let loginOutButton = QMUIButton()
        loginOutButton.setTitle(LanguageSet("退出登录"), for: .normal)
        loginOutButton.setTitleColor(BaseWhiteColor, for: .normal)
        loginOutButton.titleLabel?.font = MediumFont(16)
        loginOutButton.backgroundColor = BaseNonalTabbarColor
        return loginOutButton
    }()
    
    // 查询
    lazy var queryButton : QMUIButton = {
        let queryButton = QMUIButton()
        queryButton.setTitle(LanguageSet("查询"), for: .normal)
        queryButton.setTitleColor(BaseWhiteColor, for: .normal)
        queryButton.titleLabel?.font = MediumFont(16)
        queryButton.backgroundColor = BaseNonalTabbarColor
        return queryButton
    }()
}

